package com.course.business.controller.admin;

import com.course.server.dto.SectionDto;
import com.course.server.dto.PageDto;
import com.course.server.dto.ResponseDto;
import com.course.server.dto.SectionPageDto;
import com.course.server.service.SectionService;
import com.course.server.util.ValidatorUtil;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/admin/section")
@Slf4j
public class SectionController {

    private static final Logger LOG = LoggerFactory.getLogger(SectionController.class);
    public static final String BUSINESS_NAME = "小节";

    @Resource
    private SectionService sectionService;

    /**
     * 列表查询
     */
    @PostMapping("/list")
    public ResponseDto list(@RequestBody SectionPageDto sectionPageDto) {
        log.info("responseDto的值："+sectionPageDto);
        ResponseDto responseDto = new ResponseDto();
        ValidatorUtil.require(sectionPageDto.getCourseId(),"课程Id");
        ValidatorUtil.require(sectionPageDto.getChapterId(),"大章Id");
        sectionService.list(sectionPageDto);
        responseDto.setContent(sectionPageDto);
        return responseDto;
    }

    /**
     * 保存，id有值时更新，无值时新增
     */
    @PostMapping("/save")
    public ResponseDto save(@RequestBody SectionDto sectionDto) {
        // 保存校验
        ValidatorUtil.require(sectionDto.getTitle(), "标题");
        ValidatorUtil.length(sectionDto.getTitle(), "标题", 1, 50);
        ValidatorUtil.length(sectionDto.getVideo(), "视频", 1, 200);

        ResponseDto responseDto = new ResponseDto();
        sectionService.save(sectionDto);
        responseDto.setContent(sectionDto);
        return responseDto;
    }

    /**
     * 删除并且更新课程时间
     * @param sectionDto
     * @return responseDto
     */
    @DeleteMapping("/delete")
    public ResponseDto delete(@RequestBody SectionDto sectionDto) {
        log.info("sectionDto的值："+sectionDto);
        ResponseDto responseDto = new ResponseDto();
        sectionService.delete(sectionDto);
        return responseDto;
    }

    /**
     * 按照小节的id进行删除
     * @param id
     * @return
     */
    // @DeleteMapping("/delete/{id}")
    // public ResponseDto delete(@PathVariable String id) {
    //     ResponseDto responseDto = new ResponseDto();
    //     sectionService.delete(id);
    //     return responseDto;
    // }
}
